package com.geeklan.smart.ui.device;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.Toast;

import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.geeklan.base.base.BaseActivity;
import com.geeklan.smart.R;
import com.geeklan.smart.data.model.Device;
import com.geeklan.smart.data.repository.device.DeviceRepository;
import com.geeklan.smart.databinding.ActivityDeviceListBinding;
import com.geeklan.smart.ui.main.adapter.DeviceListAdapter;
import com.geeklan.smart.ui.viewmodel.device.DeviceViewModel;
import com.geeklan.smart.ui.viewmodel.device.DeviceViewModelFactory;

import java.util.ArrayList;
import java.util.List;

public class DeviceListActivity extends BaseActivity<ActivityDeviceListBinding> {
    private DeviceViewModel deviceViewModel;
    private DeviceListAdapter deviceListAdapter;
    private List<Device> deviceList = new ArrayList<>();
    @Override
    protected ActivityDeviceListBinding initBinding() {
        return ActivityDeviceListBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        deviceViewModel = new ViewModelProvider(this, new DeviceViewModelFactory(new DeviceRepository())).get(DeviceViewModel.class);

        deviceViewModel.getDeviceListLiveData().observe(this, response -> {
            dismiss();
            deviceList.addAll(response.getData());
            deviceListAdapter.notifyDataSetChanged();

            if(deviceList.isEmpty()){
                mBinding.llNotData.setVisibility(View.VISIBLE);
                mBinding.rvDeviceList.setVisibility(View.GONE);
            }else {
                mBinding.llNotData.setVisibility(View.GONE);
                mBinding.rvDeviceList.setVisibility(View.VISIBLE);
            }
        });

        deviceViewModel.getErrorLiveData().observe(this, s -> {
            dismiss();

            Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
        });
        deviceViewModel.getUserDevice();
    }

    @Override
    protected void initView() {
        mBinding.rvDeviceList.setNestedScrollingEnabled(false);
        mBinding.rvDeviceList.setLayoutManager(new LinearLayoutManager(this));
        deviceListAdapter = new DeviceListAdapter(this,deviceList);
        mBinding.rvDeviceList.setAdapter(deviceListAdapter);
        mBinding.ivBack.setOnClickListener(v -> {
            finish();
        });
    }

    @Override
    protected void onApplyWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            getWindow().setStatusBarColor(Color.WHITE);
            return insets;
        });
    }

    @Override
    public void networkStatus(boolean isConnected) {

    }

    @Override
    public void networkType(int type) {

    }

    public static void startActivity(Context context){
        Intent intent = new Intent(context, DeviceListActivity.class);
        context.startActivity(intent);
    }
}